<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('资源库选择')" />
    <th:block th:include="include :: ztree-css" />
</head>
<style>
    body{height:auto;font-family: "Microsoft YaHei";}
    button{font-family: "SimSun","Helvetica Neue",Helvetica,Arial;}
</style>
<body class="hold-transition box box-main">
<input id="treeId"   name="treeId"    type="hidden"  />
<input id="treeName" name="treeName"  type="hidden" />
<input id="transPath" name="transPath"  type="hidden" />
<input id="transRepositoryId" name="transRepositoryId"  type="hidden" />
<div class="wrapper"><div class="treeShowHideButton" onclick="$.tree.toggleSearch();">
    <label id="btnShow" title="显示搜索" style="display:none;">︾</label>
    <label id="btnHide" title="隐藏搜索">︽</label>
</div>
    <div class="treeSearchInput" id="search">
        <label for="keyword">关键字：</label><input type="text" class="empty" id="keyword" maxlength="50">
        <button class="btn" id="btn" onclick="$.tree.searchNode()"> 搜索 </button>
    </div>
    <div class="treeExpandCollapse">
        <a href="#" onclick="$.tree.expand()">展开</a> /
        <a href="#" onclick="$.tree.collapse()">折叠</a>
    </div>
    <div id="tree" class="ztree treeselect"></div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: ztree-js" />
<script th:inline="javascript">
    var prefix = ctx + "kettle/repository"
    var deptId = [[${deptId}]];
    var type = [[${type}]];
    $(function() {
        var url = prefix + "/repositoryRoot";
        var options = {
            url: url,
            expandLevel: 2,
            onClick : zOnClick
        };
        $.tree.init(options);
    });

    function zOnClick(event, treeId, treeNode) {

        var treeId = treeNode.id;
        var treeName = treeNode.name;
        var path=treeNode.title;


        $("#treeId").val(treeId);
        $("#treeName").val(treeName);
        $("#transPath").val(path);
        var pNodeId = treeId;
        if(/^[0-9]+.?[0-9]*$/.test(treeId)){
            $("#transRepositoryId").val(treeId);

        }

        console.log("pathpathpathpath::"+path+"_id:"+treeId);
        var treeObj = $.fn.zTree.getZTreeObj( "tree");
        var selectedNode = treeObj .getSelectedNodes();

        var pNode = selectedNode[0].getParentNode();
        while(!!pNode) {
            var id = pNode.id;
            pNode = pNode.getParentNode();
            if(pNode == null){
                pNodeId = id;
            }
        }


        $("#transRepositoryId").val(pNodeId);
        if(/^[0-9]+.?[0-9]*$/.test(treeId) && !selectedNode[0].isParent){
            console.log("进来了");
            qryRepoSubTree(treeId);
            return true;
        }

    }
    function qryRepoSubTree(treeId){
        $.ajax({
            type: 'POST',
            url: prefix+ "/qryRepoSubTree/"+treeId+"/"+type,
            contentType: "application/json",
            dataType: "json",
            success: function (data) {
                var treeObj = $.fn.zTree.getZTreeObj( "tree");
                var selectedNode = treeObj .getSelectedNodes();
                treeObj.addNodes(selectedNode[ 0], data);
            },
            error: function (e){
                alert("error");
            }
        });
    }
</script>
</body>
</html>
